package com.ke.jdyd.presenter;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.ke.jdyd.http.BaseResponse;
import com.ke.jdyd.http.HttpResultObservable;
import com.ke.jdyd.model.DetailConstant;
import com.ke.jdyd.model.GoodsEntity;
import com.ke.jdyd.service.DetailService;
import com.ke.jdyd.ui.DetailContract;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class DetailPresenter implements DetailContract.Presenter {

    private DetailContract.View mView;
    //负责对数据的存取操作
    private DetailModel mModel;

    private DetailService mService;

    public DetailPresenter(Bundle bundle, DetailContract.View view) {
        mModel = new DetailModel();
        mModel.setBundle(bundle);
        mModel.init();
        mView = view;
        mService = new DetailService();
    }

    @Override
    public void onViewCreated() {
        if (!TextUtils.isEmpty(mModel.getGoodId())) {
            loadData();
        } else {
            mView.showError("参数错误！", false);
        }
    }

    @Override
    public void reload() {
        loadData();
    }

    private void loadData() {
        mView.showLoading();
        mService.getGoodsDetail(mModel.getGoodId())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpResultObservable<GoodsEntity>() {
                    @Override
                    public void onSuccess(GoodsEntity result) {
                        //传回view
                        onDataLoaded(result);
                        mView.hideLoading();
                    }

                    @Override
                    public void onSuccess(GoodsEntity result, int code, String msg) {
                        mView.showError(msg, true);
                    }

                    @Override
                    public void onFailed(String msg) {
                        mView.showError(msg, true);
                    }
                });
    }

    private void onDataLoaded(GoodsEntity entity) {
        mModel.setGoodEntity(entity);

        mView.showGoodCover(entity.getGoods_cover());
        mView.showGoodInfo(entity.getGoods_name(), entity.getPrice(), entity.getPostage(), entity.getDelivery_place());
        mView.showGoodImages(entity.getGoods_pics());
    }

    private class DetailModel {
        private Bundle bundle;

        private String goodId;

        private GoodsEntity goodEntity;

        public void setBundle(Bundle bundle) {
            this.bundle = bundle;
        }

        public String getGoodId() {
            return goodId;
        }

        public GoodsEntity getGoodEntity() {
            return goodEntity;
        }

        public void setGoodEntity(GoodsEntity goodEntity) {
            this.goodEntity = goodEntity;
        }

        public void init() {
            if (bundle != null) {
                goodId = bundle.getString(DetailConstant.EXTRA_ID, "");
            }
        }
    }
}
